<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class Admin extends Base
{
    /**
     * 添加管理员
     */
    public function adminAdd($data=array()){
        try {
            //添加用户数据
            if(!empty($data)){
                $res = $this->firstOrCreate($data);
                if($res){
                    return [
                        'code'=>0,
                        'message'=>''
                    ];
                }else{
                    return [
                        'code'=>1,
                        'message'=>'添加失败'
                    ];
                }
            }else {
                return [
                    'code'=>1,
                    'message'=>'管理员数据丢失'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }

    // 获取管理员权限
    public function getPowers($admin){
        try {
            if(!$admin->roleid){ //角色ID为0时，为超级管理员
                return [
                    'code'=>0,
                    'data'=>[
                        'issuper'=>1,
                        'powers'=>[]
                    ]
                ];
            }else{
                if($admin->roleid==-1){ //如果为默认值,则无权限
                    return [
                        'code'=>0,
                        'data'=>[
                            'issuper'=>0,
                            'powers'=>[]
                        ]
                    ];
                }else{
                    $powers = AdminRole::find($admin->roleid)->powers->toArray();
                    $powerids = array_column($powers,'pid');
                    $result = (new AdminPower())->getPowerName($powerids);
                    if($result['code']){
                        return $result;
                    }
                    return [
                        'code'=>0,
                        'data'=>[
                            'issuper'=>0,
                            'powers'=>$result['data']
                        ]
                    ];
                }
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }

    //获取管理员列表
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = DB::table('admins')->leftJoin('admin_roles','admin_roles.id','=','admins.roleid');
            if(!empty($whereDates)){
                $model = $model->whereDate('admins.created_at','>=',$whereDates[0])->whereDate('admins.created_at','<',$whereDates[1]);
            }
            $where[]=['roleid','<>',0];
            $model = $model->where($where)->orderBy('admins.created_at','desc')->select('admins.*','admin_roles.rolename');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑管理员
    public function doUpdate($data){
        try {
            //存储数据
            $model = self::find($data['id']);
            $model->username = $data['username'];
            $model->roleid = $data['roleid'];
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>[]
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'编辑失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //重置密码
    public function adminReset($data){
        try {
            //存储数据
            $model = self::find($data['id']);
            $model->password = $data['password'];
            $model->token = $data['token'];
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>[]
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'重置失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //改变管理员状态
    public function adminStatus($data){
        try {
            //存储数据
            $model = self::find($data['id']);
            if($data['status']){
                $model->deleted_at = null;
            }else{
                $model->deleted_at = date('Y-m-d H:i:s');
            }
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>[]
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'操作失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }
}
